(function(){
	var api = 'http://h6.duchengjiu.top/shop/';
	
	var userName = document.getElementById('username');
	var passWord = document.getElementById('password');
	var nameMsg = document.getElementById('name-msg');
	var psdMsg = document.getElementById('psd-msg');
	var oModal = document.getElementById('modal');
	var loginBtn = document.getElementById('login');
	//console.log(userName);
	//console.log(passWord);
	
	loginBtn.onclick = function(){
		var userNameV = userName.value;
		var passWordV = passWord.value;
		console.log(userNameV);
		console.log(passWordV);
		$.post(api+'api_user.php',{'status':'login','username':userNameV,'password':passWordV},function(data){
//			console.log(data);
			if(data.code ==2002){
				nameMsg.innerHTML = '用户名不存在';
				var timers;
				timers = setInterval(function(){
					nameMsg.innerHTML = '';
					userName.value = '';
					passWord.value = '';
					clearInterval(timers);
				},2000);
			}else if(data.code ==1001){
				psdMsg.innerHTML = '密码错误';
				var timer;
				timer = setInterval(function(){
					psdMsg.innerHTML = '';
					passWord.value = '';
					clearInterval(timer);
				},2000);
			}else if(data.code ==0){
				localStorage.setItem('token',data.data.token);
				localStorage.setItem('username',data.data.username);
				//alert('chenggong');
				//console.log(localStorage.getItem('token'));
				//console.log(localStorage.getItem('username'));
//				var n=3;
//				oModal.style.display = 'block';
//				oModal.innerHTML = '登录成功，'+n+'秒之后跳转到首页';
//				var tim;
				
					
				var goodsId = getUrlValue('goods_id');
				if(goodsId){
					location.href = 'productdetail.html?goods_id='+goodsId;
					console.log('productdetail');
				}else{
					var n=3;
					oModal.style.display = 'block';
					oModal.innerHTML = '登录成功，'+n+'秒之后跳转到首页';
					var tim;
					tim = setInterval(function(){
						n--;
						if(n<=0){
							clearInterval(tim);
							location.href = 'index.html';
//									console.log('index');
						};
						oModal.innerHTML = '登录成功，'+n+'秒之后跳转到首页';
					},1000);
					
					
				};
			};
		});
	};
})();
//
//
//
//
///
//
//
//
//

//
//
//
//

//
//console.log(window.location)
//这个方法用来干 什么的？
function getUrlValue(name){//?goods_id=257557&age=haha
		var str = location.search.substr(1);//goods_id=257557
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var result = str.match(reg);
		console.log(result)
		//console.log(result);
		if(result === null) return null;
		return result[2];
};